const Mock = require('mockjs')
// 生成随机的表格数据项
function generateTableRow() {
    return {
        id: Mock.Random.id(),
        name: Mock.Random.cname(),
        age: Mock.Random.integer(18, 60),
        address: Mock.Random.county(true),
        createTime: Mock.Random.datetime()
    };
}

// 模拟表格数据列表
const tableDataList = [];
for (let i = 0; i < 10; i++) {
    tableDataList.push(generateTableRow());
}

// 获取表格数据的接口
const getTableData = {
    url: '/tableData',
    type: 'get',
    response: function (req) {
        return {
            code: 200,
            data: tableDataList
        };
    }
};

// 添加表格数据的接口
const addTableData = {
    url: '/addTableData',
    type: 'post',
    response: function (req) {
        const newData = req.body;
        newData.id = Mock.Random.id();
        tableDataList.push(newData);
        return {
            code: 200,
            message: '添加成功'
        };
    }
};

// 修改表格数据的接口
const updateTableData = {
    url: '/updateTableData',
    type: 'post',
    response: function (req) {
        const updatedData = req.body;
        const index = tableDataList.findIndex(item => item.id === updatedData.id);
        if (index!== -1) {
            tableDataList[index] = updatedData;
            return {
                code: 200,
                message: '修改成功'
            };
        } else {
            return {
                code: 500,
                message: '未找到要修改的数据'
            };
        }
    }
};

// 删除表格数据的接口
const deleteTableData = {
    url: '/deleteTableData',
    type: 'post',
    response: function (req) {
        const idToDelete = req.body.id;
        const index = tableDataList.findIndex(item => item.id === idToDelete);
        if (index!== -1) {
            tableDataList.splice(index, 1);
            return {
                code: 200,
                message: '删除成功'
            };
        } else {
            return {
                code: 500,
                message: '未找到要删除的数据'
            };
        }
    }
};

module.exports = [
    getTableData,
    addTableData,
    updateTableData,
    deleteTableData
];